package osiris;

import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;



import org.w3c.dom.NodeList;

import osiris.utils.OsirisXMLUtils;

public class OsirisMenu {
	    
      
    public static List<MenuItem> getMenu(){
            
            List<MenuItem> items=new ArrayList<MenuItem>();
            Hashtable<NamedNodeMap, String> attributes=new Hashtable<NamedNodeMap, String>();
    
            File xmlFile=new File(OsirisGlobalConstants.MENU_FILE_FULL_NAME);
            attributes=OsirisXMLUtils.getTagListFromFile(xmlFile, "MENU");
            
            Enumeration<NamedNodeMap> e = attributes.keys();
            NamedNodeMap obj;
            while (e.hasMoreElements()) {
            	MenuItem item=new MenuItem();
                obj = (NamedNodeMap)e.nextElement();
                System.out.println(obj.getNamedItem("TEXT").getNodeValue());
               // item.setText(obj.getNamedItem("TEXT").getNodeValue());
               // item.setAction(obj.getNamedItem("ACTION").getNodeValue());
               // items.add(item);
      }
            
            return items;
    }
    
    public static void getMenuTree()
    {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance ( );
            Document xmlDocument = null;
            
            Hashtable<NamedNodeMap, String > retAttributes=new Hashtable<NamedNodeMap, String >();
            
            File xmlFile=new File(OsirisGlobalConstants.MENU_FILE_FULL_NAME);
            
            try
            {
                    
                    DocumentBuilder builder = dbFactory.newDocumentBuilder();
                    xmlDocument = builder.parse( xmlFile );
                    NodeList listServersNodes=xmlDocument.getChildNodes();
                    
                    recorrer (listServersNodes);
                   
            }
            catch (Exception spe)
            {
               spe.printStackTrace();
                       }
            }
       
    private static void recorrer(NodeList listServersNodes){
    	
    	 for(int i=0;i<listServersNodes.getLength();i++){
    		NamedNodeMap attributes = listServersNodes.item(i).getAttributes();
    		
    		for (int j=0;j<attributes.getLength();j++){
    			System.out.println(attributes.toString());
    		}
    		     		
     		NodeList listServersNodes2=listServersNodes.item(i).getChildNodes();
     		recorrer(listServersNodes2);
       } 
    	
    }
}
